/**
 *
 * <p>Title: PlaytimeRecTableModel.java</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004 Sun Microsystems, Inc.</p>
 * <p>Company: Sun Microsystems, Inc</p>
 * @author Jeff Kesselman
 * @version 1.0
 */
package com.worldwizards;

import java.util.ArrayList;
import java.util.List;

import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;

/**
 *
 * <p>Title: PlaytimeRecTableModel.java</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004 Sun Microsystems, Inc.</p>
 * <p>Company: Sun Microsystems, Inc</p>
 * @author Jeff Kesselman
 * @version 1.0
 */
public class PlaytimeRecTableModel extends AbstractTableModel {
    List<HeroPlaytimeRecord>recList = new ArrayList<HeroPlaytimeRecord>();
    static final String[] colName = {"Character Name","Body Taken","Stun Taken"};
    /* (non-Javadoc)
     * @see javax.swing.table.TableModel#getColumnCount()
     */
    
   
    public void addPlaytimeRecord(HeroPlaytimeRecord rec){
        recList.add(rec);
        this.fireTableRowsInserted(recList.size()-1, recList.size()-1);
    }
    
    public void removePlaytimeRecord(HeroPlaytimeRecord rec){
        int idx = recList.indexOf(rec);
        recList.remove(rec);
        this.fireTableRowsDeleted(idx,idx);
    }
    
    public void clear(){
        recList.clear();
    }
    public int getColumnCount() {        
        return colName.length;
    }

    /* (non-Javadoc)
     * @see javax.swing.table.TableModel#getRowCount()
     */
    public int getRowCount() {
        // TODO Auto-generated method stub
        return recList.size();
    }

    /* (non-Javadoc)
     * @see javax.swing.table.TableModel#getValueAt(int, int)
     */
    public Object getValueAt(int rowIndex, int columnIndex) {
        HeroPlaytimeRecord rec = recList.get(rowIndex);
        if (rec == null){
            return null;
        }
        switch (columnIndex){
            case 0: return rec;
            case 1: return new Integer(rec.getBodyTaken());
            case 2: return new Integer(rec.getStunTaken());                                    
        }
        return null;
    }
    
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    @Override
    public String getColumnName(int column) {        
        return colName[column];
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        // TODO Auto-generated method stub
        return (rowIndex<recList.size());
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        HeroPlaytimeRecord rec = recList.get(rowIndex);
        switch(columnIndex){
            case 1:
                rec.takeDamage(0,Integer.parseInt((String)aValue)-rec.getBodyTaken());
                break;
            case 2:
                rec.takeDamage(Integer.parseInt((String)aValue)-rec.getStunTaken(),0);
                break;
        }
        this.fireTableCellUpdated(rowIndex, columnIndex);
        // its *possible* this changed the name color so fire that too
        this.fireTableCellUpdated(rowIndex, 0);
    }

    /**
     * @param row
     * @return
     */
    public HeroPlaytimeRecord getHeroPlaytimeRecord(int row) {        
        return recList.get(row);
    }

	public void fireRecordUpdated(HeroPlaytimeRecord rec) {
		int row = getRecordRow(rec);
		fireTableRowsUpdated(row, row);
		
	}

	private int getRecordRow(HeroPlaytimeRecord rec) {
		return recList.indexOf(rec);
	}
    
   

   

}
